/*
 * @Description: 千位分隔符
 * @Author: zzj
 * @Date: 2021-09-10 14:15:38
 * @LastEditors: zzj
 * @LastEditTime: 2021-09-14 21:30:36
 */

/*
Var num = 123456789;
封装函数 render(num);结果：123,456,789
*/
var num = 123456789;
function render(num){
    var str = num.toString();
    var arr = [];
    for(i in str){
        arr.push(str[i]); 
        if((i+1) % 3 == 0){
            arr.push(",");
        }
    }
    arr.pop();
    var str1 = arr.join("");
    console.log(str1);
}
render(num);

function parseToMoney(num) {
    num = parseFloat(num.toFixed(3));
    let [integer, decimal] = String.prototype.split.call(num, '.');
    integer = integer.replace(/\d(?=(\d{3})+$)/g, '$&,');
    return integer + '.' + (decimal ? decimal : '');
}
console.log(parseToMoney(num));